package cn.gene.timeyan.ums.webapi.ums.service.impl;

import cn.gene.timeyan.common.exception.TimeyanServiceException;
import cn.gene.timeyan.common.restful.JsonResult;
import cn.gene.timeyan.common.restful.ResponseCode;
import cn.gene.timeyan.pojo.ums.dto.UserDetailAddDTO;
import cn.gene.timeyan.pojo.ums.model.UserDetail;
import cn.gene.timeyan.pojo.ums.vo.UserInfoVo;
import cn.gene.timeyan.pojo.ums.vo.UserSimpleVO;
import cn.gene.timeyan.ums.service.IUserDetailService;
import cn.gene.timeyan.ums.webapi.ums.mapper.UserDetailMapper;
import cn.gene.timeyan.ums.webapi.ums.mapper.UserDetailMapper2;
import cn.gene.timeyan.ums.webapi.ums.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * 用户详细（不常用）信息表 服务实现类*
 *
 * @author LWG
 * @date 2022/7/22 17:28
 * @since 1.0.0@gene.cn
 */
@DubboService
@Service
@Slf4j
public class UserDetailServiceImpl implements IUserDetailService {
    /*@Autowired
    private UserDetailMapper userDetailMapper;

    @Override
    public void getLoverState(Long userId, Long loversId) {
        Long loversID = userDetailMapper.selectLoversId(userId);
        if (loversID != null) {
            throw new TimeyanServiceException(ResponseCode.ERR_UNIQUE, "您已经存在情侣搭档！");
        }else{
            userDetailMapper.updateLoversId(userId,loversId);
        }



        if(){
            UserDetail userDetail=new UserDetail();
            userDetail.setUserId(loversId);
            userDetail.setLoversId(userId);
            userDetailMapper.updateLoversId();
        }else {
            userDetailMapper.updateLoversId1(userId);
        }

    }*/

    @Autowired(required = false)
    UserDetailMapper userDetailMapper;

    @Autowired
    UserDetailMapper2 userDetailmapper2;

    @Autowired
    UserMapper userMapper;
    /**
     * 查询用户积分
     * @param userId
     * @return
     */
    @Override
    public JsonResult<Integer> selectUserScore(Long userId) {
        if (userId==null){
            throw new TimeyanServiceException(ResponseCode.ERR_NOTIME,"服务器忙");
        }
        Integer score=userDetailMapper.selectUserScore(userId);
        if (score==null){
            throw new TimeyanServiceException(ResponseCode.ERR_NOTIME,"服务器忙");
        }
        return JsonResult.ok(score);
    }
    @Transactional
    @Override
    public void addnew(UserDetailAddDTO userDetailAddDTO) {
        log.info("yyyyyyyy,{}",userDetailAddDTO);
        //1.检测用户名
        String username = userDetailAddDTO.getNickname();
        UserDetail userDetailed = userDetailMapper.getByNickName(username);
        if(userDetailed!=null){
            throw new TimeyanServiceException(ResponseCode.ERR_CATEGORY_NAME_DUPLICATE,"用户名已经存在");
        }
        //2.插入数据
        //创建user对象
        UserDetail userDetail = new UserDetail();
        //赋值
        BeanUtils.copyProperties(userDetailAddDTO,userDetail);
        /*User user = userMapper.getByPhone(phone);
        userDetail.setUserId(user.getUserId());*/
        //调用usermapper.insert(user)插入数据
        int rows = userDetailMapper.insert(userDetail);
        if(rows!=1){
            throw new TimeyanServiceException(ResponseCode.ERR_NOTIME,"服务器忙,稍后再试");
        }
    }

    @Override
    public UserSimpleVO selectUserCommentVOByUserId(Long userId) {
        //获取用户昵称和头像
        UserSimpleVO userCommentVO=userDetailMapper.selectUserCommentVOByUserId(userId);
        return userCommentVO;
    }

    /**
     * 获取用户简单信息
     * @param userId 用户id
     * @return 一个用户详情vo类
     */
    @Override
    public UserInfoVo getUserInfoVo(Long userId) {
        return userDetailmapper2.UserInfoVo(userId);

    }


    /**
     * 修改用户信息
     * @param userId
     * @param nickname
     * @param gender
     * @param birthday
     * @param address
     * @param profession
     * @param school
     * @param hobby
     * @param introduction
     *
     * @return 影响行数
     */
    @Override
    public int updateUserDetailInformationService(Long userId,
                                                  String nickname, String gender,
                                                  Date birthday, String address,
                                                  String profession, String school,
                                                  String hobby, String introduction
    ) {
        UserInfoVo user = userDetailmapper2.UserInfoVo(userId);
        if (user == null){
            throw new TimeyanServiceException(ResponseCode.BAD_REQUEST,
                    "找不到要修改的对象（" + ResponseCode.BAD_REQUEST.getValue() + "），请稍后再次尝试！");


        }

        LocalDateTime time = LocalDateTime.now();
        int row =  userDetailmapper2.updateUserDetailInformation(userId,
                nickname, gender,
                birthday, address,
                profession, school,
                hobby, introduction,time
        );
        if(row!=1){
            throw new TimeyanServiceException(ResponseCode.INTERNAL_SERVER_ERROR,
                    "删除失败,服务器忙（" + ResponseCode.INTERNAL_SERVER_ERROR.getValue() + "），请稍后再次尝试！");
        }
        return row;
    }
    @Override
    public UserDetail getNicknameAndAvatar(Long userId) {
        UserDetail user=userDetailMapper.getByUserId(userId);
        return user;
    }




}
